<?php

namespace frontend\models;

/**
 * @author  shuang
 * @date    2016-10-27 16:56:44
 * @version V1.0
 * @desc    
 */
use common\models\Vtasklist;
use Yii;
use yii\db\Query;
use frontend\models\WB_UserTaskOperate;
use yii\helpers\ArrayHelper;

class WB_Vtasklist extends Vtasklist {
    /*
     * 任务列表
     * @params $status
     * @params $title
     * return array
     */

    public static function getList($status, $title) {
        //置顶
        if (Yii::$app->request->get("page") > 1) {
            $topinfo = ["topdata" => [], "topids" => []];
        } else {
            $topinfo = self::getTopTask();
        }
        $query = new Query();
        $query->select("a.*,b.stacount,c.hidcount,d.comcount");
        $query->from(WB_Vtasklist::tableName() . " as a");
        $query->leftJoin("(SELECT COUNT(`me_user_task_operate`.`id`) as stacount,taskid FROM " . WB_UserTaskOperate::tableName() . "  WHERE status= 1 GROUP BY taskid) as b", "a.id=b.taskid");
        $query->leftJoin("(SELECT COUNT(`me_task_hidden`.`id`) as hidcount,taskid FROM " . \frontendm\models\WB_TaskHidden::tableName() . "  WHERE hidden_time= 1 GROUP BY taskid) as c", "a.id=c.taskid");
        $query->leftJoin("(SELECT COUNT(`me_user_task_operate`.`id`) as comcount,taskid FROM " . WB_UserTaskOperate::tableName() . "  WHERE is_complain= 2 GROUP BY taskid) as d", "a.id=d.taskid");
        $query->where("userid=:userid", [":userid" => Yii::$app->user->id]);
        if ($status > 0) {
            $query->andWhere("status=:status", [":status" => $status]);
        }
        if ($title) {
            $query->andFilterWhere(["like", "title", $title]);
        }
        $sort = new \yii\data\Sort([
            'attributes' => [
                "reflash_at" => [
                    'label' => '创建时间',
                ]
            ],
            'defaultOrder' => ["reflash_at" => SORT_DESC]
        ]);
        isset($topinfo["topids"]) && $topinfo["topids"] ? $query->andWhere("a.id not in (" . implode(",", $topinfo["topids"]) . ")") : null;
        $countQuery = clone $query;
        $pagesize = 15;
        $offset = (Yii::$app->request->get("page") - 1) * $pagesize;
        $limit = $pagesize;
        $query->orderBy($sort->orders);
        $pager = new \yii\data\Pagination(['totalCount' => $countQuery->count(), 'defaultPageSize' => $pagesize]);
        $res = $query->offset($offset)->limit($limit)->all();
        return ["pager" => $pager, "data" => $res, "topdata" => $topinfo["topdata"], "sort" => $sort];
    }

    public static function getTopTask() {
        $temp = ["topdata" => [], "topids" => []];
        //查找置顶任务ID
        $result = \frontendm\models\WB_TaskTop::find()->select("taskid")->orderBy("created_at desc")->asArray()->all();
        if ($result) {
            $topid = ArrayHelper::map($result, "taskid", "taskid");
            $query = new Query();
            $query->select("a.*,b.stacount,c.hidcount,d.comcount");
            $query->from(WB_Vtasklist::tableName() . " as a");
            $query->leftJoin("(SELECT COUNT(`me_user_task_operate`.`id`) as stacount,taskid FROM " . WB_UserTaskOperate::tableName() . "  WHERE status= 1 GROUP BY taskid) as b", "a.id=b.taskid");
            $query->leftJoin("(SELECT COUNT(`me_user_task_operate`.`id`) as hidcount,taskid FROM " . WB_UserTaskOperate::tableName() . "  WHERE is_hidden= 1 GROUP BY taskid) as c", "a.id=c.taskid");
            $query->leftJoin("(SELECT COUNT(`me_user_task_operate`.`id`) as comcount,taskid FROM " . WB_UserTaskOperate::tableName() . "  WHERE is_complain= 2 GROUP BY taskid) as d", "a.id=d.taskid");
            $query->andWhere("a.id in (" . implode(",", $topid) . ")");
            $query->andWhere("a.userid=".Yii::$app->user->id);
            $query->orderBy("reflash_at desc");
            $temp = ["topdata" => $query->all(), "topids" => $topid];
        }
        return $temp;
    }

}
